package com.franklin.java.action.sql.run;

import com.franklin.plugins.common.collection.PrettyArrayList;
import com.franklin.plugins.common.collection.PrettyHashSet;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.xml.XmlTag;

import javax.swing.*;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @author Franklin
 * @since 2021/9/22 16:40
 */
public abstract class MybatisSql<T extends PsiElement> {

    /**
     * 参数列表
     */
    protected Set<MybatisParam> paramList = new PrettyHashSet<>();

    protected String sql;

    protected final T sqlElement;

    protected MybatisSql(T sqlElement) {
        this.sqlElement = sqlElement;
        resolveParams();
    }

    protected abstract void resolveParams();

    public static MybatisSql getSql(PsiElement psiElement){
        if (psiElement instanceof XmlTag){
            return new MybatisXmlSql((XmlTag) psiElement);
        }else if (psiElement instanceof PsiMethod){
            return new MybatisMapperMethodSql((PsiMethod) psiElement);
        }
        throw new RuntimeException();
    }

    public List<MybatisParam> getParamList() {
        return new PrettyArrayList<>(this.paramList);
    }

    /**
     * 解析SQL并给参数填值<br>
     * @author Franklin
     * @since 2021/9/23 11:35
     * @param elExpressionComponentMap:
     * @return java.lang.String
     */
    public abstract String resolveSqlFromForm(Map<String, JComponent> elExpressionComponentMap);

    /**
     * mapper方法id<br>
     * @author Franklin
     * @since 2021/9/23 11:35
     * @param :
     * @return java.lang.String
     */
    public abstract String getMapperMethodName();
}
